package com.qiaotongtianxia.loans.activity;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.qiaotongtianxia.loans.R;
import com.qiaotongtianxia.loans.functionInterface.IBaseRequestImp;
import com.qiaotongtianxia.loans.net.Api;
import com.qiaotongtianxia.loans.util.CommonUtils;
import com.qiaotongtianxia.loans.util.Constants;
import com.qiaotongtianxia.loans.util.ToastUtil;
import com.qiaotongtianxia.loans.views.BaseTextView;
import com.qiaotongtianxia.loans.views.ButtonObserver;

import java.io.File;
import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by lisen on 2017/12/16.
 *
 * @author lisen < 4533548588@qq.com >
 */

public class QRCodeActivity extends BaseActivity {

    private final int SDCARD = 100;

    @BindView(R.id.tv_nav_title)
    BaseTextView tvNavTitle;
    @BindView(R.id.btn_save)
    ButtonObserver btnSave;
    @BindView(R.id.iv_code)
    ImageView ivCode;

    private File qrFile;
    @Override
    public int getLayoutId() {
        return R.layout.activity_qrcode;
    }

    @Override
    public void onBaseCreate(@Nullable Bundle savedInstanceState) {
        ivCode.post(new Runnable() {
            @Override
            public void run() {
                ViewGroup.LayoutParams layoutParams = ivCode.getLayoutParams();
                layoutParams.height = ivCode.getMeasuredWidth();
            }
        });
        api.QRCode(new IBaseRequestImp<String>() {
            @Override
            public void onRequestSuccess(String path) {
                btnSave.setExtraEnable(true);
                Glide.with(QRCodeActivity.this).load(path).crossFade().into(ivCode);
            }
        });

    }

    @Override
    public Api onApiCreate() {
        return new Api(this);
    }

    @Override
    public void initTitle() {
        tvNavTitle.setText("还款码");
    }

    @OnClick({R.id.iv_nav_back, R.id.btn_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_nav_back:
                finish();
                break;
            case R.id.btn_save:
                requestPermission(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},SDCARD);
                break;
        }
    }
    @Override
    public void permissionSuccess(int requestCode) {
        if (requestCode ==SDCARD){
            File file = new File(Constants.APP_FILE_DIR);
            if (!file.exists()){
                file.mkdirs();
            }
            File qrFile = new File(file,"qr.png");
            if (qrFile.exists()){
                qrFile.delete();
                try {
                    qrFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            Bitmap qRBitmap = CommonUtils.getBitmapFormView(ivCode);
            CommonUtils.saveBitmapToSdcard(qRBitmap,qrFile);

            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri = Uri.fromFile(qrFile);
            intent.setData(uri);
            sendBroadcast(intent);

            ToastUtil.showShort(this,"二维码已保存");
        }

    }

    @Override
    public void permissionFail(final int requestCode) {
        if (requestCode == SDCARD) {
            AlertDialog dialog = new AlertDialog.Builder(this)
                    .setTitle("提示信息")
                    .setMessage("无法获取权限")
                    .setNegativeButton("知道了", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .setPositiveButton("再次获取", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            requestPermission(
                                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, requestCode);
                        }
                    }).show();
            dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(this,R.color.gray));
            dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(this,R.color.mainColor));
        }
    }
}
